package com.jl.tools.crypto;

import com.jl.js.core.ByteArray;
import com.jl.security.Crypto;
import com.jl.util.HexUtil;

public class CryptoControl {
	
	
	private CryptoFrm frm;

	public CryptoControl(CryptoFrm frm){
		this.frm = frm;
	}
	
	public void run(){
		String alg = frm.getAlg().trim();
		String mode = frm.getMode().trim();
		String pad = frm.getPadding().trim();
		String descript = alg + "/" + mode + "/" + pad;
		String data = frm.getInput();
		String key = frm.getKey();
		String iv = frm.getIV();
		String result = "";
		
		if(frm.isEncrpt()){
			result = HexUtil.toHexString(Crypto.encrypt(descript, new ByteArray(key), new ByteArray(data), new ByteArray(iv)));
		}else{
			result = HexUtil.toHexString(Crypto.decrypt(descript, new ByteArray(key), new ByteArray(data), new ByteArray(iv)));
		}
		frm.setResult(result);
	}

	public static String getDefaultIV(String alg) {
		if(alg.trim().equalsIgnoreCase("AES")){
			return "00 00 00 00 00 00 00 00 " +  "00 00 00 00 00 00 00 00";
		}else{
			return "00 00 00 00 00 00 00 00";
		}
		
	}
	
	public static String[] getSupportPadMode(String alg){
		if (alg.trim().equalsIgnoreCase("AES")) {
			return new String[] { "NoPadding", "PKCS5Padding" };
		} else {
			return new String[] { "NoPadding", "ISO9797M2", "ISO9797M1",
			"PKCS5Padding" };
		}
	}
}
